//
//  Myshoucang.m
//  qulvyou
//
//  Created by mmyy on 15/10/23.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "Myshoucang.h"

#import "CollectImage.h"
@interface Myshoucang ()<UIAlertViewDelegate>

@end

@implementation Myshoucang
{
    NSMutableArray *_dataSource;
    UIScrollView *_scrollView;
    UIButton *_cover;
    CGRect rect;
    UIImageView *currentImge;
    UIButton *rightBt;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    self.navigationItem.title = @"我的收藏";
    self.automaticallyAdjustsScrollViewInsets = NO;
    _dataSource = [NSMutableArray arrayWithArray:[CollectImage getAllDate]];
    rightBt = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    [rightBt setTitle:@"删除" forState:UIControlStateNormal];
    [rightBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIBarButtonItem *_rightb = [[UIBarButtonItem alloc] initWithCustomView:rightBt];
    [rightBt addTarget:self action:@selector(deleteImage) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = _rightb;
    rightBt.hidden = YES;
    [self createScrollView];
    [self createCover];
    // Do any additional setup after loading the view.
}
-(void)deleteImage
{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否删除图片" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSString *vid = [[_dataSource objectAtIndex:currentImge.tag - 10] vid];
        [CollectImage removeDateWithVid:vid];
        [self btClick:_cover];
        
        [_scrollView removeFromSuperview];
        [_dataSource removeAllObjects];
        [_dataSource addObjectsFromArray:[CollectImage getAllDate]];
//        NSLog(@"%lu", _dataSource.count);
//
        [self createScrollView];
    }
}
//创建遮罩层
-(void)createCover
{
    _cover = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _cover.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_cover];
    _cover.alpha = 0;
    [_cover addTarget:self action:@selector(btClick:) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)btClick:(UIButton *)sender
{
    [UIView animateWithDuration:1.0 animations:^{
       sender.alpha = 0.0;
        currentImge.frame = rect;
        rightBt.hidden = YES;
    }];
    
}
-(void)createScrollView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height - 65)];
    [self.view addSubview:_scrollView];
    _scrollView.backgroundColor = [UIColor clearColor];
    CGFloat imageW = (self.view.frame.size.width - 20) / 3;
    for (int i = 0; i < _dataSource.count; i++) {
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(5 + i % 3 * (imageW + 5), 5 + i / 3 * (150 + 5), imageW, 150)];
        UIImage *image = [UIImage imageWithData:[[_dataSource objectAtIndex:i] data]];
        imageV.image = image;
        [_scrollView addSubview:imageV];
        imageV.tag = i + 10;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapClick:)];
        [imageV addGestureRecognizer:tap];
        imageV.userInteractionEnabled = YES;
    }

}
-(void)TapClick:(UITapGestureRecognizer *)sender
{
    if (_cover.alpha == 0.0) {
//
        currentImge = (UIImageView *)sender.view;
        rect = sender.view.frame;
        CGFloat viewW = self.view.bounds.size.width;
        CGFloat imageW = viewW;
        CGFloat imageH = imageW;
        CGFloat imageY = (self.view.bounds.size.height - imageH) * 0.5;
        [UIView animateWithDuration:1.0f animations:^{
            _cover.alpha = 0.1;
            sender.view.frame = CGRectMake(0, imageY, imageW, imageH);
            sender.view.center = self.view.center;
            [_scrollView bringSubviewToFront:sender.view];
            rightBt.hidden = NO;
        }];
        
    }else {
        [UIView animateWithDuration:1.0f animations:^{
            sender.view.frame = rect;
            _cover.alpha = 0.0;
            rightBt.hidden = YES;
            
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
